package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//就诊人管理接口
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    /**
     * 获取就诊人信息
     */
    //获取就诊人列表
    @GetMapping("auth/findAll")
    public R findAll(HttpServletRequest request) {

        //获取登录用户的id
        Long userId = AuthContextHolder.getUserId(request);
        //调用service来查询登录用户下的就诊人信息
        List<Patient> list = patientService.findAllPatient(userId);

        return R.ok().data("list",list);
    }


    //添加就诊人
    @PostMapping("auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request) {

        //获取userid
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        //调用service实现添加
        patientService.savePatient(patient);
        return R.ok();
    }

    //根据id获取就诊人信息
    @GetMapping("auth/get/{id}")
    public R getPatient(@PathVariable Long id) {

        Patient patient = patientService.findByIdPatient(id);
        return R.ok().data("patient",patient);
    }

    //根据id获取就诊人信息
    @GetMapping("auth/get1/{id}")
    public Patient getPatient1(@PathVariable Long id) {

        Patient patient = patientService.findByIdPatient(id);
        return patient;
    }

    //修改就诊人
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient) {

        patientService.updatePatient(patient);
        return R.ok();
    }

    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public R removePatient(@PathVariable Long id) {
        patientService.deletePatient(id);
        return R.ok();
    }

}
